package com.jms.controller.cms;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jms.controller.base.BaseController;
import com.jms.pageModel.base.Grid;
import com.jms.pageModel.base.Json;
import com.jms.pageModel.base.PageFilter;
import com.jms.pageModel.base.Tree;
import com.jms.pageModel.cms.Cate;
import com.jms.service.base.ServiceException;
import com.jms.service.cms.CateServiceI;

@Controller
@RequestMapping("/cate")
public class CateController extends BaseController {

	@Autowired
	private CateServiceI cateService;
	
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		return "/admin/cate";
	}
	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree(HttpSession session) {
		return cateService.tree();
	}
	@RequestMapping("/treeGrid")
	@ResponseBody
	public List<Cate> treeGrid() {
		return cateService.treeGrid();
	}
	@RequestMapping("/allTree")
	@ResponseBody
	public List<Tree> allTree(boolean flag) {//true获取全部资源,false只获取菜单资源
		return cateService.allTree(flag);
	}
	@RequestMapping("/getname")
	@ResponseBody
	public List<Cate> getName(Cate cate, PageFilter ph) {
		List<Cate> lc = cateService.dataGrid(cate, ph);
		
		return lc;
	}
	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(Cate cate, PageFilter ph) {
		Grid grid = new Grid();
		grid.setRows(cateService.dataGrid(cate, ph));
		grid.setTotal(cateService.count(cate, ph));
		return grid;
	}
	
	
	@RequestMapping("/addCate")
	public String addCate(HttpServletRequest request) {
		return "/admin/cateAdd";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(HttpServletRequest request,Cate cate) {
		Json j = new Json();
		try {
			Cate c = cateService.get(cate.getPid());
			String level = c.getLevel();
			if(c.getChilds()!=null&&c.getChilds().size()>0){
				int i = c.getChilds().size()+1;
				String s = "";
				if(i<10){s="0"+i;}
				else{s=Integer.toString(i);}
				cate.setLevel(level+s);
			}
			cateService.add(cate);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(Long id) {
		Json j = new Json();
		try {
			cateService.delete(id);
			j.setMsg("删除成功！");
			j.setSuccess(true);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/editCate")
	public String editCate(HttpServletRequest request, Long id) {
		Cate cate = cateService.get(id);
		request.setAttribute("cate", cate);
		return "/admin/cateEdit";
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Cate cate) {
		Json j = new Json();
		try {
			cateService.edit(cate);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (ServiceException e) {
			j.setMsg(e.getMessage());
		}
		return j;
	}
	@RequestMapping("/get")
	@ResponseBody
	public Cate get(Long id) {
		return cateService.get(id);
	}
}
